package com.metacube.questionbank.service.impl;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import com.metacube.questionbank.dao.QuestionDAO;
import com.metacube.questionbank.dao.TagDAO;
import com.metacube.questionbank.model.Question;
import com.metacube.questionbank.model.Tag;
import com.metacube.questionbank.service.QuestionService;

/*
 * @author Team Neeraj Jain
 * */
public class QuestionServiceImpl implements QuestionService {
	@Autowired
	QuestionDAO questionDAO;
	@Autowired
	VoteServiceImpl voteService;
	@Autowired
	TagServiceImpl tagService;
	@Autowired
	TagDAO tagDao;

	/*
	 * This method returns all the questions that are asked by any user.
	 * 
	 * @return list of questions
	 */
	public List<Question> getQuestions() {
		return questionDAO.getQuestions();
	}

	/*
	 * @param qId id of question This method returns the question by its id.
	 * 
	 * @return question as per the id.
	 */
	public Question getQuestionByQId(int qId) {
		return questionDAO.getQuestionByQId(qId);
	}

	/*
	 * @param objQuestion object of Question class This method inserts question
	 * in the database.
	 */
	public void insertQuestion(Question objQuestion) {
		questionDAO.insertQuestion(objQuestion);
	}

	/*
	 * @param query query typed by user. This method returns a list of related
	 * questions on the basis of query written by user.This method finds the
	 * tags in the query and finds the questions that are related to those tags.
	 * 
	 * @return list of related questions.
	 */
	public List<Question> getRelatedQuestions(String query) {
		List<Question> relatedQuestion = new ArrayList<Question>();
		List<Tag> tags = tagService.getTags();
		List<String> tagInQuery = new ArrayList<String>();
		String[] separateWord = query.split(" ");
		for (int arrCounter = 0; arrCounter < separateWord.length; arrCounter++) {
			for (int listCounter = 0; listCounter < tags.size(); listCounter++) {
				if (separateWord[arrCounter].equalsIgnoreCase(tags.get(
						listCounter).getTagName())) {
					tagInQuery.add(separateWord[arrCounter]);
				}
			}
		}
		for (int counter = 0; counter < tagInQuery.size(); counter++) {
			relatedQuestion = questionDAO.getRelatedQuestionById(
					tagInQuery.get(counter), relatedQuestion);
		}
		return relatedQuestion;
	}

	/*
	 * This method returns the maximum question id from the database
	 * 
	 * @return maxQId
	 */
	public int getMaxQId() {
		int maxQId = questionDAO.getMaxQId();
		return maxQId;
	}

	/*
	 * This method returns a list of questions which are unanswered.
	 * 
	 * @return list of questions.
	 */
	public List<Question> getUnAnsweredQuestions() {
		return questionDAO.getUnAnsweredQuestions();
	}

	/*
	 * @param userId id of user This method returns a list of questions that are
	 * posted by a particular user.
	 * 
	 * @return list of questions
	 */
	public List<Question> getQuestionGivenByUser(int userId) {
		return questionDAO.getQuestionsGivenByUser(userId);
	}
}
